<template>
  <Upload :on-success="upload" />
</template>

<script>
import { setStaffList } from '@/api/staff'
export default {
  data() {
    return {
    }
  },
  methods: {
    async upload(data) {
      const { results } = data
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const staffList = results.map((user) => {
        const staffInfo = {}
        for (const key in user) {
          const enkey = userRelations[key]
          if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
            const timeStr = user[key] = this.formatDateExcel(user[key], '/')
            user[key] = new Date(timeStr)
          }
          staffInfo[enkey] = user[key]
        }
        return staffInfo
      })
      await setStaffList(staffList)
      this.$message.success('导入成功')
      this.$router.back()
    },
    formatDateExcel(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style>

</style>
